package com.example.elmmerchants.First_Page_Fragment;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import androidx.fragment.app.Fragment;

import com.example.elmmerchants.Message_Setting.Message_Setting_Page;
import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Message_Page extends Fragment {

    View root;
    ImageButton setting_page;

    String email, merchant_custom_name, message, busy_state_auto_message;
    int is_open_online_contact, is_open_finish_auto_send_message;
    boolean is_finish_loading_book_order_data;
    AlertDialog dialog;

    public Message_Page(String email) {
        this.email = email;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_message__page, container, false);

        setting_page = root.findViewById(R.id.setting_page);
        setting_page.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                set_dialog();
                is_finish_loading_book_order_data = false;

                ExecutorService executorService = Executors.newSingleThreadExecutor();
                executorService.execute(() -> {
                    try {
                        Connection_SQL userSQL = new Connection_SQL();
                        Connection connection = userSQL.connection();
                        String sql = "SELECT * FROM merchant_message_setting WHERE email = ?;";
                        PreparedStatement preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setString(1, email);
                        ResultSet set = preparedStatement.executeQuery();
                        while (set.next()) {
                            is_open_online_contact = set.getInt("is_open_online_contact");
                            merchant_custom_name = set.getString("merchant_contact_name");
                            is_open_finish_auto_send_message = set.getInt("is_finish_auto_send_message");
                            message = set.getString("finish_auto_send_what_message");
                            busy_state_auto_message = set.getString("busy_state_auto_message");
                            is_finish_loading_book_order_data = true;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });

                while (!is_finish_loading_book_order_data) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                dialog.dismiss();
                Intent intent = new Intent(root.getContext(), Message_Setting_Page.class);
                intent.putExtra("email", email);
                intent.putExtra("merchant_custom_name", merchant_custom_name);
                intent.putExtra("message", message);
                intent.putExtra("busy_state_auto_message", busy_state_auto_message);
                intent.putExtra("is_open_online_contact", is_open_online_contact);
                intent.putExtra("is_open_finish_auto_send_message", is_open_finish_auto_send_message);
                startActivity(intent);
            }
        });

        return root;
    }

    public void set_dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(root.getContext());
        LayoutInflater inflater1 = LayoutInflater.from(root.getContext());
        builder.setView(inflater1.inflate(R.layout.loading, null));
        builder.setCancelable(false);

        dialog = builder.create();
        dialog.show();
    }
}